class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr)
{
    return false;
}
        stack<pair<TreeNode*,int>> q;
        q.push(pair<TreeNode*,int> (root,root->val));
        while(!q.empty()){
            pair<TreeNode*,int> m=q.top();
            q.pop();
            if(m.first->left==nullptr&&m.first->right==nullptr&&m.second==targetSum) return true;
            if(m.first->right){
                q.push(pair<TreeNode*,int>(m.first->right,m.second+m.first->right->val));
            }
            if(m.first->left){
                q.push(pair<TreeNode*,int>(m.first->left,m.second+m.first->left->val));
            }
        }
        return false;
    }
};